/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.homullus;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.tree.DefaultTreeCellEditor.DefaultTextField;


/**
 *
 * @author Gary
 */
public class PoseControl extends JPanel
{
    FlowLayout layout;

    JLabel poseName;

    JLabel pitchLabel;
    JLabel yawLabel;
    JLabel roleLabel;

    JTextField pitchField;
    JTextField yawField;
    JTextField roleField;
    
    JButton pitchFieldUp;
    JButton yawFieldUp;
    JButton roleFieldUp;

    Timer pitchUpRepeat;
    Timer yawUpRepeat;
    Timer roleUpRepeat;

    JButton pitchFieldDown;
    JButton yawFieldDown;
    JButton roleFieldDown;

    Timer pitchDownRepeat;
    Timer yawDownRepeat;
    Timer roleDownRepeat;

    ArrayList<PoseControl> childControls = null;

    private final int repeatTime =  200;

    private PoseAction act = null;

    public PoseControl(String name)
    {
        init(name);
    }

    public void addPoseAction(PoseAction a)
    {
        act = a;
    }


    public void setPoseDirection(PoseDirection d)
    {
        pitchField.setText(Integer.toString(d.getPitch()));
        yawField.setText(Integer.toString(d.getYaw()));
        roleField.setText(Integer.toString(d.getRole()));
    }

    public PoseDirection getPoseDirection()
    {
        PoseDirection temp = new PoseDirection();

        temp.setPitch(Integer.parseInt(pitchField.getText()));
        temp.setYaw(Integer.parseInt(yawField.getText()));
        temp.setRole(Integer.parseInt(roleField.getText()));

        return temp;
    }

    private void init(String name)
    {
        layout = new FlowLayout(layout.LEFT);

        poseName = new JLabel(name);
        poseName.setPreferredSize(new Dimension(90,20));

        //pitchLabel = new JLabel("Pitch:");
        pitchField = new JTextField("511");
        pitchField.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                callAction();
            }
        });
        pitchFieldUp = new JButton(">");
        pitchFieldUp.setFont(new Font("Arial",Font.PLAIN,12));
        pitchFieldUp.setBorder(null);
        pitchFieldUp.setPreferredSize(new Dimension(20,20));

        pitchUpRepeat = new Timer(repeatTime, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                pitchField.setText(IncreaseValue(pitchField.getText()));
                callAction();
            }
        });

        pitchFieldUp.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                pitchField.setText(IncreaseValue(pitchField.getText()));
                callAction();
                pitchUpRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                pitchUpRepeat.stop();
            }

        });

        pitchDownRepeat = new Timer(repeatTime, new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                pitchField.setText(DecreaseValue(pitchField.getText()));
                callAction();
            }
        });

        pitchFieldDown = new JButton("<");
        pitchFieldDown.setFont(new Font("Arial",Font.PLAIN,12));
        pitchFieldDown.setBorder(null);
        pitchFieldDown.setPreferredSize(new Dimension(20,20));
        pitchFieldDown.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                pitchField.setText(DecreaseValue(pitchField.getText()));
                callAction();
                pitchDownRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                pitchDownRepeat.stop();
            }

        });


        //yawLabel = new JLabel("Yaw:");
        yawField = new JTextField("511");
        yawField.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                callAction();
            }
        });

        yawUpRepeat = new Timer(repeatTime, new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                yawField.setText(IncreaseValue(yawField.getText()));
                callAction();
            }
        });

        yawFieldUp = new JButton(">");
        yawFieldUp.setFont(new Font("Arial",Font.PLAIN,12));
        yawFieldUp.setBorder(null);
        yawFieldUp.setPreferredSize(new Dimension(20,20));
        yawFieldUp.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                yawField.setText(IncreaseValue(yawField.getText()));
                callAction();
                yawUpRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                yawUpRepeat.stop();
            }


        });

        yawDownRepeat = new Timer(repeatTime, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                yawField.setText(DecreaseValue(yawField.getText()));
                callAction();
            }
        });
        
        yawFieldDown = new JButton("<");
        yawFieldDown.setFont(new Font("Arial",Font.PLAIN,12));
        yawFieldDown.setBorder(null);
        yawFieldDown.setPreferredSize(new Dimension(20,20));
        yawFieldDown.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                yawField.setText(DecreaseValue(yawField.getText()));
                callAction();
                yawDownRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                yawDownRepeat.stop();
            }

        });

        //roleLabel = new JLabel("Role:");
        roleField = new JTextField("511");
        roleField.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                callAction();
            }
        });

        roleUpRepeat = new Timer(repeatTime, new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                roleField.setText(IncreaseValue(roleField.getText()));
                callAction();
            }
        });

        roleFieldUp = new JButton(">");
        roleFieldUp.setFont(new Font("Arial",Font.PLAIN,12));
        roleFieldUp.setBorder(null);
        roleFieldUp.setPreferredSize(new Dimension(20,20));
        roleFieldUp.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                roleField.setText(IncreaseValue(roleField.getText()));
                callAction();
                roleUpRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                roleUpRepeat.stop();
            }
        });

        roleDownRepeat = new Timer(repeatTime, new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                roleField.setText(DecreaseValue(roleField.getText()));
                callAction();
            }
        });

        roleFieldDown = new JButton("<");
        roleFieldDown.setFont(new Font("Arial",Font.PLAIN,12));
        roleFieldDown.setBorder(null);
        roleFieldDown.setPreferredSize(new Dimension(20,20));
        roleFieldDown.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                roleField.setText(DecreaseValue(roleField.getText()));
                callAction();
                roleDownRepeat.start();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                roleDownRepeat.stop();
            }

        });

        setLayout(layout);

        add(poseName);

        //add(pitchLabel);
        add(pitchField);
        add(pitchFieldDown);
        add(pitchFieldUp);

        //add(yawLabel);
        add(yawField);
        add(yawFieldDown);
        add(yawFieldUp);

        //add(roleLabel);
        add(roleField);
        add(roleFieldDown);
        add(roleFieldUp);
        
    }

    private String IncreaseValue(String val)
    {
        int value = Integer.parseInt(val);
        value += 4;
        if (value > 1023)
            value = 1023;
        return Integer.toString(value);
    }

    private String DecreaseValue(String val)
    {
        int value = Integer.parseInt(val);
        value -= 4;
        if (value < 0)
            value = 0;
        return Integer.toString(value);
    }

    private void callAction()
    {
        if (act != null)
            act.ClickAction();
    }

}
